//全局对象
var welObj = {};
$(document).ready(function () {	
//	getSmsList();
//	window.setInterval(function () {
//        getSmsList();
//	}, 2000);
});

/**
 * 创建未读提醒
 * @param dataObj
 * @returns {String}
 */
function buildSmsHtml(dataObj){
	welObj.serialNo = 5;  //初始化显示长度
	$('#dropdown_alerts').empty();
	$('#bell_num').html(dataObj.length);
	//保存
	welObj.smsList = dataObj;
	var len = welObj.serialNo;
	var noReadLen = 0;
	if(len >= dataObj.length){
		len = dataObj.length;
	}
	var html = "";
	for(var i=0;i<len;i++){
		html += bulidSmgHtml(dataObj[i]);
		html += "<li class=\"divider\" id=\"divider_"+i+"\"></li>";
	}
	//多于指定的个数
	if(len < dataObj.length){
		html += bulidMoreHtml();
	}
	$('#dropdown_alerts').html(html);
}

function listMoreSms(){
	$('#msgLi').addClass('open');
	$('#msgA').attr('aria-expanded','true');
  	var smsList = welObj.smsList;
	var len = welObj.smsList.length;
	
	if(welObj.serialNo+5<len){
		len = welObj.serialNo+5;
	}
	
	var html = "";
	for(var i=0;i<len;i++){
		html += bulidSmgHtml(welObj.smsList[i]);
		html += "<li class=\"divider\" id=\"divider_"+i+"\"></li>";
	}
	//多于指定的个数
	if(welObj.serialNo+5 <  welObj.smsList.length){
		html += bulidMoreHtml();
	}
	$('#dropdown_alerts').html(html);
	
	welObj.serialNo = len;
	
	//重新加载JS
	jQuery.getScript("/web/asset/js/contabs.js")
    .done(function() {
      /* 耶，没有问题，这里可以干点什么 */
    })
    .fail(function() {
      /* 靠，马上执行挽救操作 */
    });
}

function bulidSmgHtml(obj){
	var createDate = obj.createTime;
    createDate = createDate.replace(/-/g,"/");						
    var createDate = new Date(createDate);		
	var createDate = createDate.getFullYear() + "-" + (createDate.getMonth()+1) + "-" + createDate.getDate();
	var jumpUrl = contextPath+"/welcome/updateMsg.do?id="+obj.id;
	var html = "<li>" +                 		
        		"<div>"+
        		"<i class=\"fa fa-envelope fa-fw\"></i> "+
				"<strong>"+obj.summary+"</strong>&nbsp;"+
				"<a class=\"J_menuItem\" onclick=readeMsg(\""+obj.id+"\")>"+obj.message+"</a>"+
        		"<span class=\"pull-right text-muted small\">"+createDate+"</span>"+                       		
				"</div>"+             		
        	"</li>";
	return html;
}

function bulidMoreHtml(){
	var html = "<li id='moreLi'>"+
				    "<div class=\"text-center link-block\">"+
				    	"<a href=\"javascript:listMoreSms();\">"+
				     		"<strong>查看更多 </strong>"+
				     		"<i class=\"fa fa-angle-right\"></i>"+
				    	"</a>"+
				    "</div>"+
				"</li>";
	return html;
}

function readeMsg(id){
	LocalAjax({
        url:contextPath+'/welcome/updateMsg.do?id='+id,
        async: false,
        error: function(request) {
        	layer.alert("异常错误");
	    },
        success: function(data) {
        	$('#bell_num').html(parseInt($('#bell_num').html()-1));
        }
    });
}